/**
 * 
 */
package vn.lumi.smarthome.network;

import vn.lumi.smarthome.LumiHome;
import vn.lumi.smarthome.io.database.TBL_DeviceScene;
import vn.lumi.smarthome.io.database.TBL_DeviceState;
import vn.lumi.smarthome.io.database.TBL_Scene;
import vn.lumi.smarthome.ulti.Preference;
import android.content.Context;
import android.database.Cursor;

/**
 * @author Chu Tuan Anh
 * 
 */
public class UpdateSceneDeleted extends HCMsg {

	// variables
	private int newConfigVersion = 0;
	private int userId = 0;
	private int NumOfScenes = 0;
	private int[] scenesId = null;
	private boolean isCorrectFormat = false;

	// byte position
	private static final int userIdPos = 2;

	/**
	 * @param cmd_id
	 * @param Msg_id
	 * @param len
	 */
	public UpdateSceneDeleted(int userId) {
		super(NetMessage.CMD_HC_UPDATE_SCENE_DELETED, 0);
		// TODO Auto-generated constructor stub
		this.userId = userId;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see vn.lumi.smarthome.network.HCMsg#parseMessage(byte[], int)
	 */
	@Override
	public void parseMessage(byte[] input, int length) {
		// TODO Auto-generated method stub
		isCorrectFormat = isCorrectFormat(input, length);
		if (isCorrectFormat == false)
			return;

		this.newConfigVersion = Byte2Unsigned(input[1]);
		this.userId = Byte2Unsigned(input[2]);
		this.NumOfScenes = Byte2Unsigned(input[3]);
		this.scenesId = new int[this.NumOfScenes];
		for (int i = 0; i < NumOfScenes; i++) {
			this.scenesId[i] = Byte2Unsigned(input[4 + i]);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see vn.lumi.smarthome.network.HCMsg#doWhenReceived(android.content.Context)
	 */
	@Override
	public void doWhenReceived(Context context) {
		// TODO Auto-generated method stub
		// del Scene
		for (int sceneID : scenesId) {
			String selection = new String(TBL_Scene._ID + "=?");
			String[] selectionArgs = new String[] { sceneID + "" };
			context.getContentResolver().delete(TBL_Scene.CONTENT_URI_SCENE,
					selection, selectionArgs);

			// find state ids
			selection = new String(TBL_DeviceScene.SCENE_ID + "=?");
			String[] projection = new String[] { TBL_DeviceScene.STATE_ID };
			Cursor cursor = context.getContentResolver().query(
					TBL_DeviceScene.CONTENT_URI_DEVICE_SCENE, projection,
					selection, selectionArgs, null);
			if (cursor.getCount() == 0)
				continue;

			for (cursor.moveToNext(); cursor.isAfterLast(); cursor.moveToNext()) {
				int stateID = cursor.getInt(cursor
						.getColumnIndex(TBL_DeviceScene.STATE_ID));
				String selection1 = new String(TBL_DeviceState._ID + "=?");
				String[] selectionArgs1 = new String[] { stateID + "" };
				// del device state
				context.getContentResolver().delete(
						TBL_DeviceState.CONTENT_URI_DEVICE_STATE, selection1,
						selectionArgs1);
			}

			// del scene device
			context.getContentResolver().delete(
					TBL_DeviceScene.CONTENT_URI_DEVICE_SCENE, selection,
					selectionArgs);
			if (cursor != null) {
				cursor.close();
			}
		}

		// save config in preferences
		LumiHome.HC_Config_ver = this.newConfigVersion;
		Preference.saveConfigVersion(context, this.newConfigVersion);
	}

	@Override
	public boolean isCorrectFormat(byte[] input, int length) {
		boolean result = false;
		if (super.isCorrectFormat(input, length) == false)
			result = false;
		else {
			if (this.userId == Byte2Unsigned(input[userIdPos]))
				result = true;
			else
				result = false;
		}
		return result;
	}
}
